const app = getApp();
import * as API_Members from '../../api/members'
import * as API_Distribution from '../../api/distribution'
import { Foundation } from '../../ui-utils/index.js'

Page({
  data: {
    showType: 0,
  
    params: {
      pageNo: 1,
      pageSize: 20
    },
    pointsList:[],
    finished: false,
    showGoTop: false,
    scrollTop: 0,//滚动高度
    scrollHeight: '',
    pageCount: 0,
    msg: '',
    type:null
  },
  onShow() {
    this.loadPage();
  },
  onHide() {
	this.setData({
		pointsList: []
	})
  },
  onLoad: function (options) {
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight -42
    })
    //console.log(options,14444)
    let type = options.type;
    this.setData({type:type});
  },
  loadPage() {
    
    this.getFansList();
    if (this.data.type == 1) {
      wx.setNavigationBarTitle({
        title: "铁粉"
      });
    }
    if (this.data.type == 2) {
      wx.setNavigationBarTitle({
        title: "粉丝"
      });
    }
    if (this.data.type == 3) {
      wx.setNavigationBarTitle({
        title: "缘粉"
      });
    }
  },
  getFansList() {
    if(this.data.type == 3){
      console.log('缘分')
      API_Distribution.getYuanFans({
        pageNo: this.data.params.pageNo,
      pageSize: this.data.params.pageSize
      }).then(response=>{
        const { data } = response;
        if (!data || !data.length) {
          this.setData({finished:true,msg:'没有更多内容了'})
        } else {
          data.forEach(key=>{
            key.create_time = Foundation.unixToDate(key.create_time)
          })
          this.setData({ pointsList: this.data.pointsList.concat(data)})
          if(response.data_total<response.pageSize){
            this.setData({finished:true,msg:'没有更多内容了'})
          }
        }
      })
    }else{  
      API_Distribution.getFansList({
        type: this.data.type,
        pageNo: this.data.params.pageNo,
        pageSize: this.data.params.pageSize
      }).then(response=>{
        console.log(response)
        const { data } = response;
        if (!data || !data.length) {
          this.setData({finished: true,msg:'没有更多内容了'})
        } else {
          data.forEach(key=>{
            key.create_time = Foundation.unixToDate(key.create_time)
          })
          this.setData({ pointsList: this.data.pointsList.concat(data)})
          if(response.data_total<response.pageSize){
            this.setData({finished:true,msg:'没有更多内容了'})
          }
        }
      })
    }
  },

  loadMore: function () {
    if (!this.data.finshed) {
      this.setData({ "params.pageNo": this.data.params.pageNo + 1 })
      this.getFansList(this.data.showType==1?0:1)
    }
  },
  scroll: function (e) {
    let that = this
    if (e.detail.scrollTop > 200) {
      that.setData({showGoTop: true})
    } else {
      that.setData({showGoTop: false})
    }
  },
  //返回顶部
  goTop: function () { this.setData({ scrollTop: 0 })}
})